package com.park.service.impl;

import com.park.bean.Manager;
import com.park.dao.IManagerDao;
import com.park.service.IManagerService;
import com.park.util.DigestUtil;
import com.park.util.JWTUtil;
import com.park.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Service
public class ManagerServiceImpl implements IManagerService {
    @Autowired
    IManagerDao dao;
    public final static String INITIAL_PASS="123456";

    private boolean eqPassword(String password,String password_data){
        return password.equals(password_data);
    }

    @Override
    public String doLogin(Manager manager) {
        //1.获取数据库中的数据
        List<Manager> list=dao.getManagerByUsername(manager.getUsername());
        if(list.size()==1){
            Manager manager_data=list.get(0);
        //2.比较密码
            boolean bool=DigestUtil.decode(manager.getPassword(),manager_data.getPassword());
            if(bool){
/*                if(manager_data.getInitial()==0){
                    return "需要重置密码";
                }else */
                //3.生成token
                return JWTUtil.createToken(null);

            }
        }

        return null;
    }

    @Override
    public List<Manager> getManager(Manager manager, Page page) {
        return dao.getManager(manager,page);
    }

    @Override
    public int getCount(Manager manager) {
        return dao.getCount(manager);
    }

    @Override
    public boolean checkUserExist(Manager manager) {
        List<Manager> list=dao.getManagerByUsername(manager.getUsername());
        if(list.isEmpty()){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean changeCheck(Manager manager) {
        List<Manager> list=dao.getManagerByUsername(manager.getUsername());
        if(list.size()==1){
            Manager manager_data=list.get(0);
            //比较密码
            boolean bool=DigestUtil.decode(manager.getPassword(),manager_data.getPassword());
            if(bool){
                return true;
            }
        }
        return false;

    }

    @Override
    public int postManager(Manager manager) {
        manager.setPassword(DigestUtil.randomDigest(INITIAL_PASS));
        return dao.postManager(manager);
    }

    @Override
    public int delManager(int id) {
        return dao.delManager(id);
    }

    @Override
    public int editManager(Manager manager) {
        String pass=manager.getPassword();
        manager.setPassword(DigestUtil.randomDigest(pass));
        return dao.editManager(manager);
    }
    @Override
    public int changePass(Manager manager) {
        String pass=manager.getPassword();
        manager.setPassword(DigestUtil.randomDigest(pass));
        return dao.changePass(manager);
    }

}
